﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using EMC;



namespace SaunaServer
{
    public partial class Form1 : Form
    {
        UserManagement user = new UserManagement();
        Server mainServer;
        private delegate void UpdateStatusCallback(string strMessage);

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> usrs;
            usrs = user.ListUsers();
            int i = 0;
            lstUserTable.Items.Clear();
            foreach (string st in usrs)
            {
                lstUserTable.Items.Add(st);
                i++;
            }
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            //Obtain the IP address from txtIp text box
            IPAddress ipAddress = IPAddress.Parse(txtIp.Text);

            // Create a new instance of the ChatServer object
            mainServer = new Server(ipAddress);
            // Hook the StatusChanged event handler to mainServer_StatusChanged
            Server.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
            // Start listening for connections
            mainServer.StartListening();
            // Show that we started to listen for connections
            txtLog.AppendText("Listening for connections...\r\n");
        }

        public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            // Call the method that updates the form
            this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
        }

        private void UpdateStatus(string strMessage)
        {
            // Updates the log with the message
            txtLog.AppendText(strMessage + "\r\n");

        }

        private void btnGetIp_Click(object sender, EventArgs e)
        {
            IPHostEntry ipHostEntry = Dns.GetHostEntry(string.Empty);
            txtIp.Text = GetIP4Address();
        }

        string GetIP4Address()
        {
            string IP4Address = "0.0.0.0";
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return IP4Address;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Server.SendMessageTo("tom", "Hello tom");
            List<string> usrs;
            usrs=user.ListUsers();
            
            //mainServer.ServRunning = false;
            //mainServer.StopListening();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                mainServer.StopListening();
            }
            catch
            {

            }
            
            
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            user.delUser(txtDeleteUser.Text);
            List<string> usrs;
            usrs = user.ListUsers();
            int i = 0;
            lstUserTable.Items.Clear();
            foreach(string st in usrs)
            {
                lstUserTable.Items.Add(st);
                i++;
            }
        }

        private void timListUsers_Tick(object sender, EventArgs e)
        {
            List<string> usrs;
            usrs = user.ListUsers();
            int i = 0;
            lstUserTable.Items.Clear();
            foreach (string st in usrs)
            {
                lstUserTable.Items.Add(st);
                i++;
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
